import { get,post} from '../../../utils/request.js'
import global from '../../../utils/global.js'
import { getLocation } from '../../../utils/wxapi.js'
var city = require('../../../utils/city.js');
var app = getApp()
let that
Page({
  data: {
    searchLetter: [],
    showLetter: "",
    winHeight: 0,
    // tHeight: 0,
    // bHeight: 0,
    cityList: [],
    isShowLetter: false,
    scrollTop: 0,//置顶高度
    scrollTopId: '',//置顶id
    city: "正在定位中",
    lat:"",
    long:"",
    hotcityList: [{ cityCode: 110000, city: '北京市' }, { cityCode: 310000, city: '上海市' }, { cityCode: 440100, city: '广州市' }, { cityCode: 440300, city: '深圳市' }, { cityCode: 330100, city: '杭州市' }, { cityCode: 320100, city: '南京市' }, { cityCode: 420100, city: '武汉市' }, { cityCode: 410100, city: '郑州市' }, { cityCode: 120000, city: '天津市' }, { cityCode: 610100, city: '西安市' }, { cityCode: 510100, city: '成都市' }, { cityCode: 500000, city: '重庆市' }]
  },
  onLoad: function (options) {
    that = this;
    that.getListCity()
    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight;
    that.setData({
      winHeight: winHeight,
      city: options.city,
      lat: options.lat,
      long: options.long
    })
    if(that.data.lat&&that.data.long){
      that.reCity(that.data.lat,that.data.long);
    }else{
      that.getLocation()
    }
  },
  getLocation: function () {
    getLocation(result => {
      console.log(result)
      that.reCity(result.latitude, result.longitude)
    })

  },
  // 城市定位
  reCity( lat, long) {
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=EZueXnIGhjGOet9icCe8FORGHdTj6a5H&location=' + lat + ',' + long + '&output=json&coordtype=wgs84ll',
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: res => {
        console.log(res.data.result.addressComponent.adcode)
        wx.hideLoading()
        if (res.statusCode !== 200) {
          wx.showToast({
            title: '网络不给力！',
            icon: 'loading',
            duration: 2000
          })
        } else {
          // console.log(res.data.result)
    
          that.setData({
            city: res.data.result.addressComponent.city
          })
        }
        // wx.setStorageSync('selectCIty', res.data.result.addressComponent.city);
        // wx.redirectTo({
        //   url: '../index?code=' + res.data.result.addressComponent.adcode
        // })
      }
      
    })
    
  },
  // 获取城市列表
  getListCity(){
    post(global.apiUrl.locationPage, { }, function (res) {
      console.log(res.obj.city)
      that.setData({
        cityList: res.obj.city
      })
    })
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function () {
    // 生命周期函数--监听页面显示

  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  },
  clickLetter: function (e) {
    console.log(e.currentTarget.dataset.letter)
    var showLetter = e.currentTarget.dataset.letter;
    this.setData({
      showLetter: showLetter,
      isShowLetter: true,
      scrollTopId: showLetter,
    })
    var that = this;
    setTimeout(function () {
      that.setData({
        isShowLetter: false
      })
    }, 1000)
  },
  //选择城市
  bindCity: function (e) {
    console.log("bindCity", e)
    // this.setData({ city: e.currentTarget.dataset.city })
    wx.setStorageSync('selectCIty', e.currentTarget.dataset.city);
    wx.redirectTo({
      url: '../index?code=' + e.currentTarget.dataset.citycode 
    })
  },
  //选择热门城市
  bindHotCity: function (e) {
    console.log("bindHotCity")
    wx.setStorageSync('selectCIty', e.currentTarget.dataset.city);
    // this.setData({
    //   city: e.currentTarget.dataset.city
    // })
    wx.redirectTo({
      url: '../index'
    })
  },
  //点击热门城市回到顶部
  hotCity: function () {
    this.setData({
      scrollTop: 0,
    })
  }
})